import { defineStore } from 'pinia'
import {userLoginApi} from "@/api/loginApi"
const myadminStore = defineStore('admin', {
  persist: {
    storage: window.sessionStorage,
    paths: ['userInfo']
  },
  state: () => ({
    showTab: true,
    userInfo: {
      userid: 0,
      name: '',
      token: '',
      avatar: ''
    }
  }),
  getters: {
    showTabbar: state => state.showTab
  },
  actions: {
    // 设置用户登录后的基础信息
    setUserInfo(boolean) {
      this.showTab = boolean
    },
    setInfo(user) {
      this.userInfo = user
    },
    // 用户退出
    logoutUserInfo() {
      this.userInfo = {
        userid: 0,
        name: '',
        token: '',
        avatar: ''
      }
    },
    // 发起网络请求，进行用户登录验证
    // 一定要返回，需要在登录成功后，进行路由跳转
    async userLoginAction(userData) {
      let ret = await userLoginApi(userData)
      if (ret.code === 0) {
        this.setInfo(ret.data)
      }
      return ret.code
    }
  }
})

export default myadminStore